import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/app_permission/permission_widget.dart';
import 'package:yt_devops/components/brn/brn_bottom_button_panel.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/add_mains_electricity_price/views/effective_date_widgets.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/add_mains_electricity_price/views/item_tow_checkbox_widget.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/elec_price_title_widget.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/energy_storeage_discharge_price/widgets/electricity_energy_storage_poriods.dart';
import 'package:yt_devops/pages/site/site_setting/site_setting_base_info/widgets/item_widget.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/constants/permission_page_constant.dart';
import 'energy_storeage_discharge_price_controller.dart';

class EnergyStoreageDischargePricePage extends GetView<EnergyStoreageDischargePriceController> {
    const EnergyStoreageDischargePricePage({Key? key}) : super(key: key);

    @override
    Widget build(BuildContext context) {
    return GetBuilder<EnergyStoreageDischargePriceController>(
        init: controller,
        builder: (controller) {
            return Scaffold(
            resizeToAvoidBottomInset: true,
            backgroundColor: Colour.backgroundColor,
            appBar: AppBarWidget.withRightActions(
                ResKey.siteSettingAddStoreDischargePrice.tr,
                actions: [
            PermissionWidget(permissionStringList:
            const [PermissionConstant.homeSiteManageSettingElectricitySettingEssEdit],
              child:Visibility(
                        visible: !controller.isEnableEdit,
                        child: IconButton(
                            icon: Image.asset(ImageAssets.siteSettingBaseInfoEdit),
                            onPressed: controller.edit,
                        ),
                    ))
                ],
            ),

            body: Container(

                child: ListView(
                    padding: EdgeInsets.zero, children: [
                    Container(
                        height: 10,
                        color: Colour.backgroundColor,
                    ),
          Container(
          clipBehavior: Clip.hardEdge,
          margin: const EdgeInsets.only(left: 16,right: 16),
          decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8),
          color: Colors.white,
          ),
          child:Column(
          children: [ElecPriceTitleWidget(controller.titleStringList[0]),
                    ItemWidget(
                        ResKey.siteSettingElectricityRoleName.tr,
                        controller.ruleNameController,
                        unitString: '',
                        titleColor: Colour.otherTextColor,
                        inputTextColor: Colour.mainTextColor,
                        titleFont: 14,
                        isCanEdit:controller.isEnableEdit,
                        textChangeCallBack: (str){
                            controller.checkCommitStatus();
                        },
                    ),
                    ItemTowCheckboxWidget(ResKey.siteSettingElectricityEffectiveStatus.tr,
                        ResKey.siteSettingEffective.tr,
                        ResKey.siteSettingUnEffective.tr,
                        isEdit:controller.isEnableEdit,
                        value: controller.status == 0?ResKey.siteSettingEffective.tr:ResKey.siteSettingUnEffective.tr,
                        controller.status, (index) {
                        Log.d('index=' + index.toString());
                        controller.status = index;

                    })])),
                    Container(
                        height: 10,
                        color: Colour.backgroundColor,
                    ),
                    EffectiveDateWidgets(controller.timeDateList, (){
                        controller.updateTimeList();
                    }, (index){
                        controller.deleteTimeList(index);
                    },(){
                        controller.update();
                    },isEdit: controller.isEnableEdit,),
          Container(
          clipBehavior: Clip.hardEdge,
          margin: const EdgeInsets.only(left: 16,right: 16,top: 10),
          decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8),
          color: Colors.white,
          ),
          child:Column(
          children: [ElecPriceTitleWidget(controller.titleStringList[2]),

                  controller.isEnableEdit?ElectricityEnergyStoragePeriods(
                      controller.stagePriceList,(){
                    controller.updateStagePriceList();
                  },( int index){
                    controller.deleteStagePriceList(index);
                  },(){
                    controller.update();
                  },(){
                    controller.update();
                  },):
                  Container(
                    height: controller.stagePriceList.length * 48 * 3,
                    child: ListView.builder(
                      padding: EdgeInsets.zero,
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                      itemBuilder: (BuildContext context, int index) {
                        return  _itemWidget(controller.stagePriceList[index]);
                      }
                      ,
                      itemCount: controller.stagePriceList.length,
                    ),
                  )]))
                    ]
                )
            ),
                bottomNavigationBar: controller.isEnableEdit?Container(
                  color: Colour.white,
                  padding: EdgeInsets.only(bottom: ScreenUtil().bottomBarHeight),
                  child: YtBrnBottomButtonPanel(
                      enableMainButton: controller.isEnableCommit,
                      mainButtonName: ResKey.save.tr,
                      mainButtonOnTap: () => controller.save(),
                      secondaryButtonName: ResKey.cancel.tr,
                      enableSecondaryButton: true,
                      secondaryButtonBorder:
                      Border.all(color: Colour.dividerColor),
                      secondaryButtonOnTap: () =>
                          controller.cancelEdit()),
                ):null
        );
    });
    }

    _itemWidget(Map map){
      var elecType = '';
      Log.d('elecType==' + map.toString());
      if(map['elecType'] != null){
        if(map['elecType'] == 0){
          elecType = ResKey.siteSettingElectricityShape.tr;
        }else if(map['elecType'] == 1){
          elecType = ResKey.siteSettingElectricityPeak.tr;
        }else if(map['elecType'] == 2){
          elecType = ResKey.siteSettingElectricityAverage.tr;
        }else if(map['elecType'] == 3){
          elecType = ResKey.siteSettingElectricityValley.tr;
        }
      }
      GetStorage storage = GetStorage();
      String monetaryUnitName = storage.read('monetaryUnitName')??'元';
      if(monetaryUnitName == '元' || monetaryUnitName == '¥'){
        monetaryUnitName = ResKey.yuan.tr;
      }
      return Container(
        color: Colors.white,
        height: 48 * 3,
        margin: EdgeInsets.only(left: 16.w,right: 16.w),
        child: Column(
          children: [
            Container(
              height: 48,
                child:
            Row(
              children: [
                Text( ResKey.siteSettingElectricityTimeStage.tr,style: ytTextStyle(fontSize: 14,color: Colour.otherTextColor),),
                Spacer(),
                Text( map['intervalStartTime'].toString() + '-' + map['intervalEndTime'].toString(),style: ytTextStyle(fontSize: 14),),
              ],
            )),

            Container(
                height: 48,
                child:
                Row(
                  children: [
                    Text( ResKey.deviceElectrovalence.tr,style: ytTextStyle(fontSize: 14,color: Colour.otherTextColor),),
                    Spacer(),
                    Text( '${map['electricityFees']}$monetaryUnitName/kWh',style: ytTextStyle(fontSize: 14),),
                  ],
                )),
            Container(
                height: 48,
                child:
                Row(
                  children: [
                    Text( ResKey.deviceElectricityPriceType.tr,style: ytTextStyle(fontSize: 14,color: Colour.otherTextColor),),
                    Spacer(),
                    Text( elecType,style: ytTextStyle(fontSize: 14),),
                  ],
                )),
          ],
        ),
      );
    }
}
